package com.jianguo.web.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jianguo.common.core.domain.AjaxResult;
import com.jianguo.common.core.page.TableDataInfo;
import com.jianguo.order.domain.PzGroup;
import com.jianguo.order.domain.PzGroupMenber;
import com.jianguo.order.domain.PzOrder;
import com.jianguo.order.service.IPzGroupMenberService;
import com.jianguo.order.service.IPzGroupService;
import com.jianguo.order.service.IPzOrderService;
import com.jianguo.user.domain.PzRelation;
import com.jianguo.user.domain.PzUser;
import com.jianguo.user.domain.PzUserPatient;
import com.jianguo.user.domain.PzUserPatientRelation;
import com.jianguo.user.service.IPzRelationService;
import com.jianguo.user.service.IPzUserPatientRelationService;
import com.jianguo.user.service.IPzUserPatientService;
import com.jianguo.user.service.IPzUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static com.jianguo.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/api/relation")
public class RelationApi {
    @Autowired
    private IPzUserPatientRelationService pzUserPatientRelationService;
    @Autowired
    private IPzUserPatientService pzUserPatientService;
    @Autowired
    private IPzRelationService relationService;
    @Autowired
    private IPzUserService pzUserService;
    @Autowired
    private IPzGroupMenberService groupMenberService;
    @Autowired
    private IPzGroupService groupService;
    @Autowired
    private IPzOrderService orderService;
    /**
     * 新增
     */
    @PostMapping("/save")
    public AjaxResult saveRelation(@RequestBody PzRelation relation, HttpServletRequest request)
    {
        String account = request.getHeader("account");
        PzUser user = pzUserService.getOne(new LambdaQueryWrapper<PzUser>()
                .eq(PzUser::getAccount, account));

        relation.setUserId(user.getId());

        PzUser user1 = pzUserService.getOne(new LambdaQueryWrapper<PzUser>()
                .eq(PzUser::getAccount, relation.getRelationId()));

        relation.setRelationId(user1.getId());
        relation.setRemark(user1.getRemark());
        relation.setPic(user1.getPic());
        // 根据userId查找所有患者
        List<PzUserPatient> patientList = pzUserPatientService.list(new LambdaQueryWrapper<PzUserPatient>()
                .eq(PzUserPatient::getUserId, user.getId()));
        List<PzUserPatientRelation> pzUserPatientRelationList = new ArrayList<>();
        // 遍历患者列表，判断是否存在关系
        for (PzUserPatient patient : patientList) {
            String patientId = patient.getId();
            PzUserPatientRelation pzUserPatientRelation = new PzUserPatientRelation().setRelationId(user1.getId())
                    .setUserId(user.getId()).setStatus(0L).setPatientId(patientId).setAccount(user1.getAccount())
                    .setRelation(relation.getRelation()).setMobile(user1.getMobile()).setEmail(user1.getEmail())
                    .setNickname(user1.getNickname()).setPic(user1.getPic());
            pzUserPatientRelationList.add(pzUserPatientRelation);
        }
        pzUserPatientRelationService.saveBatch(pzUserPatientRelationList);
        return AjaxResult.success(relationService.save(relation));
    }

    @GetMapping("/list")
    public AjaxResult list(PzRelation pzRelation , HttpServletRequest request)
    {
        String account = request.getHeader("account");
        PzUser user = pzUserService.getOne(new LambdaQueryWrapper<PzUser>()
                .eq(PzUser::getAccount, account));
        String userId = user.getId();

        List<PzRelation> relationList = relationService.list(new LambdaQueryWrapper<PzRelation>()
                .eq(PzRelation::getUserId, userId));

        return AjaxResult.success(relationList);
    }

    @GetMapping("/list2")
    public AjaxResult list2(HttpServletRequest request)
    {
        String account = request.getHeader("account");
        PzUser user = pzUserService.getOne(new LambdaQueryWrapper<PzUser>()
                .eq(PzUser::getAccount, account));
        String userId = user.getId();

        List<PzUserPatientRelation> relationList = pzUserPatientRelationService.list(new LambdaQueryWrapper<PzUserPatientRelation>()
                .eq(PzUserPatientRelation::getUserId, userId));

        return AjaxResult.success(relationList);
    }

    @GetMapping("/change")
    public AjaxResult status(String id, Long status)
    {
        LambdaUpdateWrapper<PzUserPatientRelation> updateWrapper = new LambdaUpdateWrapper<PzUserPatientRelation>()
                .eq(PzUserPatientRelation::getId, id);
        // 根据id查询
        PzUserPatientRelation userPatientRelation = pzUserPatientRelationService.getById(id);
        String patientId = userPatientRelation.getPatientId();
        // 根据患者id查询所有订单

        List<PzOrder> orderList = orderService.list(new LambdaQueryWrapper<PzOrder>()
                .eq(PzOrder::getPatientId, patientId));
        // 根据订单id查询所有群组
        for (PzOrder order : orderList) {
            String orderId = order.getId();
            // 根据订单id查询群组
            PzGroup group = groupService.getOne(new LambdaQueryWrapper<PzGroup>()
                    .eq(PzGroup::getOrderId, orderId));
            String groupId = group.getId();
            if (status == 1L){
                // 删除群组成员
                groupMenberService.remove(new LambdaQueryWrapper<PzGroupMenber>()
                        .eq(PzGroupMenber::getGroupId, groupId).eq(PzGroupMenber::getUserId, userPatientRelation.getRelationId()));
            }else {
                // 添加群组成员
                PzGroupMenber groupMenber = new PzGroupMenber().setGroupId(groupId).setUserId(userPatientRelation.getRelationId())
                        .setNickname(userPatientRelation.getNickname()).setPic(userPatientRelation.getPic()).setUserType(2L);
                groupMenberService.save(groupMenber);
            }
        }

        if (status == 1L){
            updateWrapper.set(PzUserPatientRelation::getStatus, 0L);
        }else {
            updateWrapper.set(PzUserPatientRelation::getStatus, 1L);
        }
        return success(pzUserPatientRelationService.update(updateWrapper));
    }
}
